﻿using Ch9_Portable.Interfaces;
using Ch9_Portable.Models;
using Ch9_Portable.Services;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ch9_Portable.ViewModels
{
    public class MainViewModelBase : INotifyPropertyChanged
    {
        IDeviceDataService storageService;
        protected string savedSessionsFile = "savedLocalSessions.json";

        public MainViewModelBase() { }

        public async Task LoadInitialData(IDeviceDataService storage)
        {

            // not using the await keyword so that each 
            //   stream can be loaded asychronously 
            LoadFeatured();
            LoadSeries();
            LoadShows();
            if (storage != null)
            {
                storageService = storage;
                LoadSavedData();
            }

        }

        private async Task LoadFeatured()
        {
            var featured = await Ch9RssService.GetFeaturedShowsAsync();
            int count = 0;
            foreach (Ch9Item ch9 in featured.channel.items) {
                count++;
                ch9.Type = Ch9ItemType.Post;
                FeaturedItems.Add(ch9);
                if (count < FeatureLimitCount)
                    LimitFeaturedItems.Add(ch9);
            }
        }

        private async Task LoadSeries()
        {
            var series = await Ch9RssService.GetSeriesListAsync();
            foreach (Ch9Item ch9 in series.channel.items)
            {
                ch9.Type = Ch9ItemType.Series;
                SeriesItems.Add(ch9);
            }
                
        }

        private async Task LoadShows()
        {
            var shows = await Ch9RssService.GetShowListAsync();
            foreach (Ch9Item ch9 in shows.channel.items)
            {
                ch9.Type = Ch9ItemType.Show;
                ShowItems.Add(ch9);
            }
        }

        private async Task LoadSavedData()
        {
            var recentlyViewed = await storageService.LoadSavedData();
            RecentlyViewed.Clear();
            foreach (Ch9Item ch9 in recentlyViewed)
                RecentlyViewed.Add(ch9);
        }

        public async Task ReloadRecentData()
        {
            LoadSavedData();
        }

        public Ch9Item GetItemFromGuid(string guid)
        {
            Ch9Item returnItem = null;
            foreach(Ch9Item c in FeaturedItems)
            {
                if (c.guid == guid)
                    returnItem = c;
            }
            if(returnItem == null)
            {
                foreach(Ch9Item c in RecentlyViewed)
                {
                    if (c.guid == guid)
                        returnItem = c;
                }
            }
            return returnItem;
        }

        #region RecentlyViewed (ObservableCollection<Ch9Item>)
        private ObservableCollection<Ch9Item> _recentlyViewed = new ObservableCollection<Ch9Item>();
        public ObservableCollection<Ch9Item> RecentlyViewed
        {
            get { return _recentlyViewed; }
            set
            {
                _recentlyViewed = value;
                NotifyPropertyChanged("RecentlyViewed");
            }
        }
        #endregion

        #region FeaturedItems (ObservableCollection<Ch9Item>)
        private ObservableCollection<Ch9Item> _featuredItems = new ObservableCollection<Ch9Item>();

        public ObservableCollection<Ch9Item> FeaturedItems
        {
            get { return _featuredItems; }
            set
            {
                _featuredItems = value;
                NotifyPropertyChanged("FeaturedItems");
            }
        }
        #endregion

        #region SeriesItems (ObservableCollection<Ch9Item>)
        private ObservableCollection<Ch9Item> _seriesItems = new ObservableCollection<Ch9Item>();
        public ObservableCollection<Ch9Item> SeriesItems
        {
            get { return _seriesItems; }
            set
            {
                _seriesItems = value;
                NotifyPropertyChanged("SeriesItems");
            }
        }
        #endregion

        #region ShowItems (ObservableCollection<Ch9Item>)
        private ObservableCollection<Ch9Item> _showItems = new ObservableCollection<Ch9Item>();
        public ObservableCollection<Ch9Item> ShowItems
        {
            get { return _showItems; }
            set
            {
                _showItems = value;
                NotifyPropertyChanged("ShowItems");
            }
        }
        #endregion

        #region LimitFeaturedItems (ObservableCollection<Ch9Item>)
        private ObservableCollection<Ch9Item> _limitFeaturedItems = new ObservableCollection<Ch9Item>();
        public ObservableCollection<Ch9Item> LimitFeaturedItems
        {
            get { return _limitFeaturedItems; }
            set
            {
                _limitFeaturedItems = value;
                NotifyPropertyChanged("LimitFeaturedItems");
            }
        }
        #endregion

        #region FeatureLimitCount (int)
        private int _featureLimitCount = 10;
        public int FeatureLimitCount
        {
            get { return _featureLimitCount; }
            set
            {
                _featureLimitCount = value;
                LimitFeaturedItems.Clear();
                if(value < FeaturedItems.Count){
                    for (int i = 0; i < value; i++)
                    {
                        LimitFeaturedItems.Add(FeaturedItems[i]);
                    }
                } 
                else
                {
                    LimitFeaturedItems = FeaturedItems;
                }
                NotifyPropertyChanged("FeatureLimitCount");
            }
        }
        #endregion


        public event PropertyChangedEventHandler PropertyChanged;
        protected void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        
    }
}
